package com.gaofei.sysmanager.controller;


import com.gaofei.sysmanager.common.MsgUtil;
import com.gaofei.sysmanager.domain.EmailInfo;
import com.gaofei.sysmanager.service.EmailService;
import gui.ava.html.parser.HtmlParser;
import gui.ava.html.parser.HtmlParserImpl;
import gui.ava.html.renderer.ImageRenderer;
import gui.ava.html.renderer.ImageRendererImpl;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.Message;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import java.util.Properties;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gaofei
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/menuRole")
public class MenuRoleController {

    public static final String SenderEmail = "2390172499@qq.com";//发送人邮箱
    public static final String senderCode = "xsilkiqdvkmndijc";//发送人邮箱授权码
    public static final String emailSMTPHost = "smtp.qq.com";//服务器地址

    public static final String receiveMailAccount = "junjie0608_w@163.com";//收件人邮箱


    private static String from = "2390172499@qq.com";// 用于给用户发送邮件的邮箱
    private static String password = "xsilkiqdvkmndijc"; // 邮箱的密码
    private static String host = "smtp.qq.com";// 发送邮件的服务器地址
    private static int port = 587;


//    private String urlFile = "C:\\Users\\93173\\Documents\\WeChat Files\\wxid_7nnid3kbw9lc12\\FileStorage\\File\\2022-04\\index.html";
//    private String urlFile = "C:\\Users\\93173\\Documents\\WeChat Files\\wxid_7nnid3kbw9lc12\\FileStorage\\File\\2022-04\\01fa0b5cabed84b5948616f2c3cefd41_t.gif";
      private String urlFile = "";
//      private String urlFile = "tiebapic.baidu.com/forum/w%3D580%3B/sign=7922b1567c600c33f079dec02a77500f/ac345982b2b7d0a21eb576dfdcef76094b369a77.jpg";
    @Autowired
    private EmailService emailService;


    @GetMapping(path = "sendEmail")
    public void setMsgUtil() {

        /*spring.mail.host=smtp.qq.com
        spring.mail.username=2390172499@qq.com
         spring.mail.password=xsilkiqdvkmndijc
        spring.mail.default-encoding=utf-8
*/
        String HtmlTemplateStr = readFile("C:\\Users\\93173\\Documents\\WeChat Files\\wxid_7nnid3kbw9lc12\\FileStorage\\File\\2022-04\\demo.htm");

        EmailInfo emailInfo = new EmailInfo();

        emailInfo.setNickname("发件人姓名");
        emailInfo.setSubject("反馈");
//        emailInfo.setContent("反馈内容")
        emailInfo.setContent(HtmlTemplateStr);
        emailInfo.setAddress("zhaokangzgs@ccic-net.com.cn");
//        emailInfo.setFilePath("http://localhost:9001/uploadFile/2022/04/15/f435f896-5e1f-420a-a5e3-f12d963fe430.html");
        emailInfo.setFilePath(urlFile);

        //发送邮件
        String s = emailService.sendEmail(emailInfo);

        System.out.println(s);
    }


    @RequestMapping(path = "HttpTransformationImg",method = RequestMethod.GET)
    public void HttpTransformationImg(){
        String HtmlTemplateStr = readFile("C:\\Users\\93173\\Documents\\WeChat Files\\wxid_7nnid3kbw9lc12\\FileStorage\\File\\2022-04\\index.html");
        HtmlParser htmlParser = new HtmlParserImpl();
        htmlParser.loadHtml(HtmlTemplateStr);
        // html 是我的html代码
        ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
        imageRenderer.saveImage("E:\\lcxq1.png");
    }

    public String readFile( String filePath ){
        FileInputStream fis=null;
        String result = "" ;
        try {
            // 根据path路径实例化一个输入流的对象
            fis  = new FileInputStream( filePath );

            //2. 返回这个输入流中可以被读的剩下的bytes字节的估计值；
            int size =  fis.available() ;
            //3. 根据输入流中的字节数创建byte数组；
            byte[] array = new byte[size];
            //4.把数据读取到数组中；
            fis.read( array ) ;

            //5.根据获取到的Byte数组新建一个字符串，然后输出；
            result = new String(array);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally{
            if ( fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return result ;
    }
}

